package de.dhbw.asa.model;

import java.awt.Image;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.ResultSet;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;

@ManagedBean
@RequestScoped
public class Product extends Database
{

	public int iProductNr;
	public String sTitle;
	public String sImage;
	public float fPrice;
	public String sDescription;
	
	//for the quantity to order
	int iQuantity=1;
	
	/*
	 * this method loads a product from the database
	 */
	public boolean load(int iProductNr)
	{
		boolean bReturn = false;
		String sProductNr = String.valueOf(iProductNr);
		StringBuffer sbQuery = new StringBuffer();
		sbQuery.append("Select * from product where productNr = '");
		sbQuery.append(sProductNr);
		sbQuery.append("';");
		try
		{
			if (this.createConnection())
			{
				ResultSet jResult = this.executeQuery(sbQuery.toString());
				if (jResult.next())
				{
					this.iProductNr = iProductNr;
					this.sTitle = jResult.getString("title");
					this.sImage = jResult.getString("image");
					this.sDescription = jResult.getString("description");
					this.fPrice = jResult.getFloat("price");
				}
				bReturn = true;
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return bReturn;
	}
	
	/*
	 * this method saves changes of a product
	 */
	public String saveChanges()
	{
		String sReturn = "";
		try
		{
			this.createConnection();
			StringBuffer sbQuery = new StringBuffer();
			// Insert the product into the Database
			sbQuery.append("update product set title'=");
			sbQuery.append(this.sTitle);
			sbQuery.append("', image ='");
			sbQuery.append(this.sImage);
			sbQuery.append("', description =");
			sbQuery.append(this.sDescription);
			sbQuery.append("', price='");
			sbQuery.append(this.fPrice);
			sbQuery.append("' where productNr =");
			sbQuery.append(this.iProductNr);
			sbQuery.append(";");
			this.execute(sbQuery.toString());
			this.closeConnection();
		}
		catch (Exception e)
		{
			e.printStackTrace();
			sReturn = e.getMessage();
		}
		return sReturn;
	}
	
	/*
	 * this method saves a new product
	 */
	public String saveNewProduct(String sTitle, Image jImage, String sDecription, float fPrice)
	{
		String sReturn = "";
		this.sTitle = sTitle;
		this.sImage = sImage;
		this.sDescription = sDecription;
		this.fPrice = fPrice;
		
		try
		{
			this.createConnection();
			StringBuffer sbQuery = new StringBuffer();
			// Finding out the last ProductNr of our Products
			sbQuery.append("Select productNr from Product;");
			ResultSet jResult = this.executeQuery(sbQuery.toString());
			int iLastProductNr = 0;
			while (jResult.next())
			{
				iLastProductNr = jResult.getInt("productNr");
			}
			this.iProductNr = iLastProductNr+1;
			StringBuffer sbQuery2 = new StringBuffer();
			// Insert the product into the Database
			sbQuery2.append("insert into product (productNr, title, image, description, price) values (");
			sbQuery2.append(this.iProductNr);
			sbQuery2.append(",'");
			sbQuery2.append(this.sTitle);
			sbQuery2.append("','");
			sbQuery2.append(this.sImage);
			sbQuery2.append("','");
			sbQuery2.append(this.sDescription);
			sbQuery2.append("','");
			sbQuery2.append(this.fPrice);
			sbQuery2.append("');");
			this.execute(sbQuery2.toString());
			this.closeConnection();
		}
		catch (Exception e)
		{
			e.printStackTrace();
			sReturn = e.getMessage();
		}
		return sReturn;
	}

	public int getiProductNr() 
	{
		return iProductNr;
	}

	public void setiProductNr(int iProductNr) 
	{
		this.iProductNr = iProductNr;
	}

	public String getsTitle() 
	{
		return sTitle;
	}

	public void setsTitle(String sTitle) 
	{
		this.sTitle = sTitle;
	}

	public String getsImage() 
	{
		return sImage;
	}

	public void setsImage(String sImage) 
	{
		this.sImage = sImage;
	}

	public float getfPrice() 
	{
		return fPrice;
	}

	public void setfPrice(float fPrice) 
	{
		this.fPrice = fPrice;
	}

	public String getsDescription() 
	{
		return sDescription;
	}

	public void setsDescription(String sDescription) 
	{
		this.sDescription = sDescription;
	}

	public int getiQuantity() {
		return iQuantity;
	}

	public void setiQuantity(int iQuantity) {
		this.iQuantity = iQuantity;
	}

	
	
	
}
